Completed
Push — master ( 59cd4c...8588a0 )
by Dimas
10:00
created

sftp   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 13

5 Functions

Rating   Name   Duplication   Size   Complexity  
A connect 0 26 3
A unlinkFile 0 13 3
A getRemotePath 0 10 1
A unlinkFolder 0 13 3
A uploadFile 0 29 3
1
import * as upath from 'upath';
2
import { readFileSync } from 'fs';
3
import { Client } from 'scp2';
4
import Config from './Config';
5
import local from './local';
6
7
var Client2 = require('scp2').Client;
8
9
export default class sftp {
10
  client: Client;
11
  local: local;
12
13
  constructor(private config: Config) {
14
    this.local = new local(config);
15
  }
16
17
  connect(): Promise<string> {
18
    ///
19
    this.client = new Client({
20
      port: this.config.port,
21
      host: this.config.host,
22
      username: this.config.username,
23
      password: this.config.password,
24
      // agentForward: true,
25
      privateKey: this.config.privateKey
26
        ? readFileSync(upath.normalizeSafe(this.config.privateKey)).toString()
27
        : undefined,
28
      // debug: true
29
    });
30
    //console.log(this.client);
31
32
    // Triggers the initial connection
33
    this.client.sftp((err, sftp) => {
34
      if (err) {
35
        console.log('There was a problem with connection');
36
      }
37
    });
38
39
    return new Promise<string>((resolve, reject) => {
40
      this.client.on('ready', () => {
41
        resolve('connected');
42
      });
43
    });
44
  }
45
46
  getRemotePath(path: string): string {
47
    let normalPath = upath.normalizeSafe(path);
48
    let normalLocalPath = upath.normalizeSafe(this.config.localPath);
49
    let remotePath = normalPath.replace(
50
      normalLocalPath,
51
      this.config.remotePath
52
    );
53
    let pathJoin = upath.join(this.config.remotePath, remotePath);
54
    return pathJoin;
55
    //console.log(pathJoin);
56
    //return upath.normalizeSafe(`${this.config.remotePath}/${remotePath}`);
57
    //return upath.normalizeSafe(remotePath);
58
  }
59
60
  unlinkFile(fileName: string): Promise<string> {
61
    return new Promise<string>((resolve, reject) => {
62
      let remote = this.getRemotePath(fileName);
63
      this.client.sftp((err, sftp) => {
64
        if (err) {
65
          reject('SFTP cannot be created');
66
        } else {
67
          sftp.unlink(remote, (err) => {
68
            if (err) {
69
              reject('File could not be deleted');
70
            } else {
71
              resolve(remote);
72
            }
73
          });
74
        }
75
      });
76
    });
77
  }
78
79
  unlinkFolder(folderPath: string): Promise<string> {
80
    return new Promise<string>((resolve, reject) => {
81
      let remote = this.getRemotePath(folderPath);
82
      this.client.sftp((err, sftp) => {
83
        if (err) {
84
          reject('SFTP cannot be created');
85
        } else {
86
          sftp.rmdir(remote, (err) => {
87
            if (err) {
88
              reject('Folder could not be deleted');
89
            } else {
90
              resolve(remote);
91
            }
92
          });
93
        }
94
      });
95
    });
96
  }
97
98
  uploadFile(fileName: string): Promise<string> {
99
    return new Promise<string>((resolve, reject) => {
100
      let remote = this.getRemotePath(fileName);
101
      ///console.log(remote);
102
103
      // Client upload also creates the folder but creates it using local mode
104
      // in windows it might mean we won't have permissons to save the fileName
105
      // So I create the folder manually here to solve that issue.
106
      // Mode we set can be configured from the config file
107
      this.client.mkdir(
108
        upath.dirname(remote),
109
        { mode: this.config.pathMode },
110
        (err) => {
111
          if (err) {
112
            reject({
113
              message: `Could not create ${upath.dirname(remote)}`,
114
              error: err,
115
            });
116
          } else {
117
            // Uplad the file
118
            this.client.upload(fileName, remote, (err) => {
119
              if (err) {
120
                reject({
121
                  message: `Could not upload ${remote}`,
122
                  error: err,
123
                });
124
              } else {
125
                resolve(remote);
126
              }
127
            });
128
          }
129
        }
130
      );
131
    });
132
  }
133
}
134